home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_055 / newstartups / astartup.asm next >
Assembly Source File  |  1992-05-06  |  10KB  |  440 lines

  1. * ==========================
  2. * amiga.dev/programs #11, from cscheppner, 10335 chars, Tue Feb 24 17:40:06 1987
  3. * --------------------------
  4. * TITLE:  Astartup.asm --- with 1.2 fixes, handles quotes better
  5.  
  6. ************************************************************************
  7. *
  8. *   Astartup.asm --- C Program Startup/Exit (CLI and WorkBench)
  9. *
  10. *   Modified from the following source by cscheppner
  11. *   (commented out non-functional ToolWindow code)
  12. ************************************************************************
  13. *
  14. *
  15. *
  16. * Source Control:
  17. * --------------
  18. *
  19. * $Header: startup.asm,v 1.1 86/08/25 12:50:07 root Exp $
  20. *
  21. * $Locker: root $
  22. *
  23. * $Log:   startup.asm,v $
  24. * Revision 1.1  86/08/25  12:50:07  root
  25. * Initial revision
  26. *
  27. * Revision 33.6  86/06/12  10:34:36  neil
  28. * changed so any version of dos is OK
  29. * Revision 33.5  86/06/11  19:03:47  neil
  30. * fixed CloseLibrary if open of dos failed
  31. * Revision 33.4  86/06/09  16:48:59  neil
  32. * now processes escapes also
  33. * Revision 33.3  86/06/09  16:18:12  neil
  34. * another checkpoint -- quoted strings now work
  35. * Revision 1.1  85/11/23  13:49:01  neil
  36. * Initial revision
  37. *
  38. ************************************************************************
  39.  
  40.  
  41. ******* Included Files *************************************************
  42.  
  43.    INCLUDE   "exec/types.i"
  44.    INCLUDE "exec/alerts.i"
  45.    INCLUDE "exec/nodes.i"
  46.    INCLUDE "exec/lists.i"
  47.    INCLUDE "exec/ports.i"
  48.    INCLUDE "exec/libraries.i"
  49.    INCLUDE "exec/tasks.i"
  50.    INCLUDE "libraries/dos.i"
  51.    INCLUDE "libraries/dosextens.i"
  52.    INCLUDE "workbench/startup.i"
  53.  
  54.  
  55. ******* Imported *******************************************************
  56.  
  57. xlib   macro
  58.    xref   _LVO\1
  59.    endm
  60.  
  61.    xref   _AbsExecBase
  62.    xref   _Input
  63.    xref   _Output
  64.  
  65.    xref   _main         ; C code entry point
  66.  
  67.    xlib   Alert
  68.    xlib   FindTask
  69.    xlib   Forbid
  70.    xlib   GetMsg
  71.    xlib   OpenLibrary
  72.    xlib   CloseLibrary
  73.    xlib   ReplyMsg
  74.    xlib   Wait
  75.    xlib   WaitPort
  76.  
  77.    xlib   CurrentDir
  78.    xlib   Open
  79.    xlib   Close
  80.  
  81. ******* Exported *******************************************************
  82.  
  83.    xdef   _SysBase
  84.    xdef   _DOSBase
  85.  
  86.    xdef   _errno
  87.    xdef   _stdin
  88.    xdef   _stdout
  89.    xdef   _stderr
  90.  
  91.    xdef   _exit         ; standard C exit function
  92.    xdef   _WBenchMsg
  93.  
  94.  
  95. callsys   macro
  96.    CALLLIB   _LVO\1
  97.    endm
  98.  
  99.  
  100. ************************************************************************
  101. *
  102. *   Standard Program Entry Point
  103. *
  104. ************************************************************************
  105. *
  106. *   main (argc, argv)
  107. *      int  argc;
  108. *      char *argv[]; 
  109. *
  110. ************************************************************************
  111.  
  112. startup:            ; reference for Wack users
  113.       move.l   sp,initialSP   ; initial task stack pointer
  114.       move.l   d0,dosCmdLen
  115.       move.l   a0,dosCmdBuf
  116.  
  117.       ;------ get Exec's library base pointer:
  118.       move.l   _AbsExecBase,a6
  119.       move.l   a6,_SysBase
  120.  
  121.       ;------ get the address of our task
  122.       suba.l   a1,a1
  123.       callsys   FindTask
  124.       move.l   d0,a4
  125.  
  126.       ;------ are we running as a son of Workbench?
  127.       tst.l   pr_CLI(A4)
  128.       beq   fromWorkbench
  129.  
  130. ;=======================================================================
  131. ;====== CLI Startup Code ===============================================
  132. ;=======================================================================
  133. fromCLI:
  134.       ;------   attempt to open DOS library:
  135.       bsr   openDOS
  136.  
  137.       ;------ find command name:
  138.       suba.l   a0,a0
  139.       move.l   pr_CLI(a4),d0
  140.       lsl.l   #2,d0      ; bcpl pointer conversion
  141.       move.l   cli_CommandName(a0,d0.l),d0
  142.       lsl.l   #2,d0      ; bcpl pointer conversion
  143.  
  144.       ;------ create buffer and array:
  145.       movem.l   a2/a3,-(sp)
  146.       lea   argvBuffer,a2
  147.       lea   argvArray,a3
  148.  
  149.       ;------ fetch command name:
  150.       move.l   d0,a0
  151.       moveq.l   #0,d0
  152.       move.b   (a0)+,d0   ; size of command name
  153.       clr.b   0(a0,d0.l)   ; terminate the string
  154.       move.l   a0,(a3)+
  155.  
  156.       ;------   collect parameters:
  157.       move.l   dosCmdLen,d0
  158.       move.l   dosCmdBuf,a0
  159.  
  160.       ;------ null terminate the string, eat trailing garbage
  161.       lea   0(a0,d0.l),a1
  162. stripjunk:
  163.       cmp.b   #' ',-(a1)
  164.  
  165.       ;-- jimm: 8/25/86: per kodiak's recommendation
  166.       ; bls.s   stripjunk
  167.       dbhi   D0,stripjunk
  168.  
  169.       clr.b   1(a1)
  170.  
  171. newarg:
  172.       ;------ skip spaces
  173.       move.b   (a0)+,d1
  174.       beq.s   parmExit
  175.       cmp.b   #' ',d1
  176.       beq.s   newarg
  177.       cmp.b   #9,d1         ; tab
  178.       beq.s   newarg
  179.  
  180.       ;------ push address of the next parameter
  181.       move.l   a2,(a3)+
  182.  
  183.       ;------ process quotes
  184.       cmp.b   #'"',d1
  185.       beq.s   doquote
  186.  
  187.       ;------ copy the parameter in
  188.       move.b   d1,(a2)+
  189.  
  190. nextchar:
  191.       ;------ null termination check
  192.       move.b   (a0)+,d1
  193.       beq.s   parmExit
  194.       cmp.b   #' ',d1
  195.       beq.s   endarg
  196.  
  197.       move.b   d1,(a2)+
  198.       bra.s   nextchar
  199.  
  200. endarg:
  201.       clr.b   (a2)+
  202.       bra.s   newarg
  203.  
  204. doquote:
  205.       ;------ process quoted strings
  206.       move.b   (a0)+,d1
  207.       beq.s   parmExit
  208.       cmp.b   #'"',d1
  209.       beq.s   endarg
  210.  
  211.       ;------ '*' is the BCPL escape character
  212.       cmp.b   #'*',d1
  213.       bne.s   addquotechar
  214.  
  215.       move.b   (a0)+,d1
  216.       cmp.b   #'N',d1
  217.       beq.s   1$
  218.       cmp.b   #'n',d1
  219.       bne.s   2$
  220.  
  221. 1$:
  222.       ;------ got a *N -- turn into a newline
  223.       moveq   #10,d1
  224.       bra.s   addquotechar
  225.  
  226. 2$:
  227.       cmp.b   #'E',d1
  228.       beq.s   3$
  229.       cmp.b   #'e',d1
  230.       bne.s   addquotechar
  231.  
  232. 3$:
  233.       ;------ got a *E -- turn into a escape
  234.       moveq   #27,d1
  235.  
  236. addquotechar:
  237.       move.b   d1,(a2)+
  238.       bra.s   doquote
  239.  
  240. parmExit:
  241.       ;------ all done -- null terminate the baby
  242.       clr.b   (a2)
  243.       clr.l   (a3)
  244.  
  245.       ;------ compute the # of arguments
  246.       move.l   #dosCmdBuf,d0
  247.       sub.l   a3,d0
  248.       not.l   d0
  249.       lsr.l   #2,d0
  250.  
  251.       movem.l   (sp)+,a2/a3
  252.       pea   argvArray
  253.       move.l   d0,-(sp)
  254.  
  255.  
  256. *
  257. *  The above code relies on the end of line containing a control
  258. *  character of any type, i.e. a valid character must not be the
  259. *  last.  This fact is ensured by DOS.
  260. *
  261.       
  262.  
  263.       ;------ get standard input handle:
  264.       jsr   _Input
  265.       move.l   d0,_stdin
  266.  
  267.       ;------ get standard output handle:
  268.       jsr   _Output
  269.       move.l   d0,_stdout
  270.       move.l   d0,_stderr
  271.  
  272.       ;------ call C main entry point
  273.       jsr   _main
  274.  
  275.       ;------ return success code:
  276.       moveq.l   #0,D0
  277.       move.l   initialSP,sp   ; restore stack ptr
  278.       rts
  279.  
  280. ;=======================================================================
  281. ;====== Workbench Startup Code =========================================
  282. ;=======================================================================
  283. fromWorkbench:
  284.       ;------ open the DOS library:
  285.       bsr   openDOS
  286.  
  287.       ;------ we are now set up.  wait for a message from our starter
  288.       bsr   waitmsg
  289.  
  290.       ;------ save the message so we can return it later
  291.       move.l   d0,_WBenchMsg
  292.  
  293.       ;------ push the message on the stack for wbmain
  294.       move.l   d0,-(SP)
  295.       clr.l   -(SP)      indicate: run from Workbench
  296.  
  297.       ;------ get the first argument
  298.       move.l   d0,a2
  299.       move.l   sm_ArgList(a2),d0
  300.       beq.s   docons
  301.  
  302.       ;------ and set the current directory to the same directory
  303.       move.l   _DOSBase,a6
  304.       move.l   d0,a0
  305.       move.l   wa_Lock(a0),d1
  306.       callsys   CurrentDir
  307.  
  308. docons:
  309. * Note: This code commented out for 2 reasons.  WorkBench never passes
  310. *       the ToolWindow ptr in the WBenchMsg, and there is no matching
  311. *       cleanup for this code.   cs
  312.  
  313. *      ;------ get the toolwindow argument
  314. *      move.l   sm_ToolWindow(A2),d1
  315. *      beq.s   domain
  316.  
  317. *      ;------ open up the file
  318. *      move.l   #MODE_OLDFILE,d2
  319. *      callsys   Open
  320.  
  321. *      ;------ set the C input and output descriptors
  322. *      move.l   d0,_stdin
  323. *      move.l   d0,_stdout
  324. *      move.l   d0,_stderr
  325. *      beq.s   domain
  326.  
  327. *      ;------ set the console task (so Open( "*", mode ) will work
  328. *      ;   waitmsg has left the task pointer in A4 for us
  329. *      lsl.l   #2,d0
  330. *      move.l   d0,a0
  331. *      move.l   fh_Type(a0),pr_ConsoleTask(A4)
  332.  
  333. domain:
  334.       jsr   _main
  335.       moveq.l   #0,d0      Successful return code
  336.       bra.s   exit2
  337.  
  338.  
  339. ************************************************************************
  340. *
  341. *   C Program Exit Function
  342. *
  343. ************************************************************************
  344. *
  345. *  Warning: this function really needs to do more than this.
  346. *
  347. ************************************************************************
  348.  
  349. _exit:
  350.       move.l   4(SP),d0   ; extract return code
  351. exit2:
  352.       move.l  initialSP,SP   ; restore stack pointer
  353.       move.l   d0,-(SP)   ; save return code
  354.  
  355.       ;------ close DOS library:
  356.       move.l   _AbsExecBase,A6
  357.       move.l   _DOSBase,d0
  358.       beq.s   1$
  359.       move.l   d0,a1
  360.       callsys   CloseLibrary
  361. 1$:
  362.  
  363.       ;------ if we ran from CLI, skip workbench cleanup:
  364.       tst.l   _WBenchMsg
  365.       beq.s   exitToDOS
  366.  
  367.       ;------ return the startup message to our parent
  368.       ;------   we forbid so workbench can't UnLoadSeg() us
  369.       ;------   before we are done:
  370.       callsys Forbid
  371.       move.l   _WBenchMsg,a1
  372.       callsys   ReplyMsg
  373.  
  374.       ;------ this rts sends us back to DOS:
  375. exitToDOS:
  376.       move.l   (SP)+,d0
  377.       rts
  378.  
  379.  
  380. ;-----------------------------------------------------------------------
  381. noDOS:
  382.       ALERT   (AG_OpenLib!AO_DOSLib)
  383.       moveq.l   #100,d0
  384.       bra.s   exit2
  385.  
  386.  
  387. ;-----------------------------------------------------------------------
  388. ; This routine gets the message that workbench will send to us
  389. ; called with task id in A4
  390.  
  391. waitmsg:
  392.       lea   pr_MsgPort(A4),a0     * our process base
  393.       callsys   WaitPort
  394.       lea   pr_MsgPort(A4),a0     * our process base
  395.       callsys GetMsg
  396.       rts
  397.  
  398. ;-----------------------------------------------------------------------
  399. ;  Open the DOS library:
  400.  
  401. openDOS
  402.       lea   DOSName(pc),A1
  403.       moveq   #0,d0
  404.       callsys OpenLibrary
  405.       move.l   D0,_DOSBase
  406.       beq   noDOS
  407.       rts
  408.  
  409. DOSName      DOSNAME
  410.  
  411. ************************************************************************
  412.  
  413.    DATA
  414.  
  415. ************************************************************************
  416.  
  417. VerRev      dc.w   33,1
  418.  
  419. _SysBase   dc.l   0
  420. _DOSBase   dc.l   0
  421.  
  422. _errno      dc.l   0
  423. _stdin      dc.l   -1
  424. _stdout      dc.l   -1
  425. _stderr      dc.l   -1
  426.  
  427. initialSP   dc.l   0
  428. _WBenchMsg   dc.l   0
  429.  
  430. dosCmdLen   dc.l   0
  431. dosCmdBuf   dc.l   0
  432.  
  433. argvArray   ds.l   32
  434. argvBuffer   ds.b   256
  435.